package com.example.demo.common.mybatis;/**
 * Created by kete on 2018-06-11.
 */

import com.example.demo.common.mybatis.config.GenerateSqlmapConfigTools;
import com.example.demo.common.mybatis.config.SqlMapConfig;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 作者：kete
 * 创建时间：2018-06-11 10:58
 * 功能描述：
 * 版本：
 */
public class GeneratorSqlmapUtil {

    public void createXmlFile(){

        GenerateSqlmapConfigTools toolsMysql = new GenerateSqlmapConfigTools("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@10.145.196.101:1521:iamzwdb", "dbusr01", "dbusr01123", "iamzwdb", "D:/mapper/oracle");

        SqlMapConfig conf = new SqlMapConfig("I_SERV_STATE_LOG", "com.example.demo.blog.entity", "com.example.demo.blog.mapper");

        toolsMysql.addTable(conf);

        toolsMysql.generateXmlFile("D:/mapper/oracle/file.xml");
    }

    public void generator(String filePath) throws Exception {

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File(filePath);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

    }

    public static String parseMonth(String billMonth) throws Exception{

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

        Date date = null;
        try {
            date =sdf.parse(billMonth);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        if(date == null) throw new IllegalArgumentException("查询帐期格式错误，应为YYYYMM");

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 1);

        return sdf.format(c.getTime());
    }


    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmapUtil generatorSqlmap = new GeneratorSqlmapUtil();

            generatorSqlmap.createXmlFile();

			generatorSqlmap.generator("D:/mapper/oracle/file.xml");

//            String sb= GeneratorSqlmapUtil.parseMonth("201707");
//
//            System.out.println(sb);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
